package com.serviceprovider.business.module.admin.acl.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.acl.model.AclClass;
import com.serviceprovider.business.module.admin.acl.services.dao.interfaces.IAclClassService;
import com.serviceprovider.business.utility.AppUtil;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AclClassController {
    @Autowired
    IAclClassService                    aclClassService;
    @Autowired
    private ApplicationConfigProperties applicationConfigProperties;

    @RequestMapping(value = RequestConstants.AclRequests.ACL_CLASS)
    public ModelAndView enterAclClass() {
        ModelAndView mav = new ModelAndView(RequestConstants.AclRequests.VIEW_ACL);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.AclRequests.CREATE_ACL_CLASS,
        method = RequestMethod.GET
    )
    public ModelAndView enterCreate(HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.AclRequests.VIEW_CREATE_ACL);

        mav.addObject("aclClass", new AclClass());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.AclRequests.CREATE_ACL_CLASS,
        method = RequestMethod.POST
    )
    public ModelAndView create(@ModelAttribute("aclClass") AclClass form) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.AclRequests.VIEW_ACL_CLASS);

        aclClassService.insertAclClass(form);

        return mav;
    }

    @RequestMapping(value = RequestConstants.AclRequests.VIEW_ACL_CLASS)
    public ModelAndView view(HttpServletRequest request, HttpSession sess) {
        ModelAndView mav    = new ModelAndView(RequestConstants.AclRequests.VIEW_VIEW_ACL);
        int          pageNo = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "id"
                            : request.getParameter("sortColumn");

        mav.addObject("aclClassList",
                      aclClassService.findAll(pageNo,
                          applicationConfigProperties.getRecordSize("view_acl_classes.records.size"), sortColumn,
                          sortOrder));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.AclRequests.EDIT_ACL_CLASS,
        method = RequestMethod.GET
    )
    public ModelAndView enterEdit(HttpServletRequest req, HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.AclRequests.VIEW_EDIT_ACL);

        mav.addObject("aclClass", aclClassService.getAclClassById(Integer.parseInt(req.getParameter("id"))));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.AclRequests.EDIT_ACL_CLASS,
        method = RequestMethod.POST
    )
    public ModelAndView edit(@ModelAttribute("aclClass") AclClass form) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.AclRequests.VIEW_ACL_CLASS);

        aclClassService.updateAclClass(form);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.AclRequests.DELETE_ACL_CLASS,
        method = RequestMethod.POST
    )
    public ModelAndView deleteRoles(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.AclRequests.VIEW_ACL_CLASS);

        aclClassService.deleteAclClassesById(AppUtil.toInt(req.getParameterValues("ids")));

        return mav;
    }
}



